package com.wwwarehouse.common.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.filter.FilterMultipleAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.eventbus_event.DrawerMultipleCompleteEvent;
import com.wwwarehouse.common.eventbus_event.DrawerMultipleResetEvent;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/8/25.
 * 项目中的抽屉布局(多选)
 */

public class DeskDrawerMultipleFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private TextView mTitleTxt;
    private GridView mGridView;
    private Button mRestBtn;
    private Button mCompleteBtn;

    private String mTitle;
    private List<FilterBean> mFilterList;
    private FilterMultipleAdapter mAdapter;
    private int mColumns;

    private Set<Integer> mSelectedPositionSet;

    private static final String KEY_TITLE = "key_title";
    private static final String KEY_LIST = "key_list";
    private static final String KEY_COLUMNS = "key_columns";

    public static final DeskDrawerMultipleFragment newInstance(String title, List<FilterBean> filterList, int columns) {
        DeskDrawerMultipleFragment deskDrawerMultipleFragment = new DeskDrawerMultipleFragment();

        Bundle bundle = new Bundle();
        bundle.putString(KEY_TITLE, title);
        bundle.putSerializable(KEY_LIST, (Serializable) filterList);
        bundle.putInt(KEY_COLUMNS, columns);

        deskDrawerMultipleFragment.setArguments(bundle);

        return deskDrawerMultipleFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_desk_drawer_multiple, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mTitleTxt = findView(mRootView, R.id.tv_title);
        mGridView = findView(mRootView, R.id.gv_content);
        mRestBtn = findView(mRootView, R.id.btn_reset);
        mCompleteBtn = findView(mRootView, R.id.btn_complete);

        mSelectedPositionSet = new HashSet<>();
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mTitle = bundle.getString(KEY_TITLE);
            mFilterList = (List<FilterBean>) bundle.getSerializable(KEY_LIST);
            mColumns = bundle.getInt(KEY_COLUMNS);

            mTitleTxt.setText(mTitle);
            mGridView.setNumColumns(mColumns);

            mAdapter =  new FilterMultipleAdapter(mActivity, mFilterList);
            mGridView.setAdapter(mAdapter);
        }
    }

    private void initEvent() {
        mRestBtn.setOnClickListener(this);
        mCompleteBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_reset) {
            mSelectedPositionSet.clear();

            for (FilterBean filterBean : mFilterList) {
                filterBean.setSelect(false);
            }

            mAdapter.notifyDataSetChanged();

            EventBus.getDefault().post(new DrawerMultipleResetEvent(mSelectedPositionSet));

        } else if (id == R.id.btn_complete) {
            mSelectedPositionSet.clear();

            for (int i = 0; i < mFilterList.size(); i++) {
                if (mFilterList.get(i).isSelect()) {
                    mSelectedPositionSet.add(i);
                }
            }

            EventBus.getDefault().post(new DrawerMultipleCompleteEvent(mSelectedPositionSet));

            ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
        } else {
        }
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }
}
